package com.heitian.ssm.controller;

import java.util.Date;

import com.alibaba.fastjson.JSONObject;
import com.heitian.ssm.dao.UserDao;
import com.heitian.ssm.model.Thing;
import org.fusesource.mqtt.client.Future;
import org.fusesource.mqtt.client.FutureConnection;
import org.fusesource.mqtt.client.MQTT;
import org.fusesource.mqtt.client.Message;
import org.fusesource.mqtt.client.QoS;
import org.fusesource.mqtt.client.Topic;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.Resource;
import javax.json.JsonObject;

public class Demo {

//	private  static UserDao userDao;
//
//	public Demo(UserDao userDao) {
//		this.userDao = userDao;
//	}

	private final static String CONNECTION_STRING = "tcp://139.224.24.250:1883";
	private final static boolean CLEAN_START = true;
	private final static short KEEP_ALIVE = 30;// 低耗网络，但是又需要及时获取数据，心跳30s
	public final static long RECONNECTION_ATTEMPT_MAX = -1;
	public final static long RECONNECTION_DELAY = 2000;

	public final static int SEND_BUFFER_SIZE = 2 * 1024 * 1024;// 发送最大缓冲为2M
	
	public static void main(String[] args) {

		mqttstart("XDK110/zffxdk/Data/Stream");
	}


	public static Thing mqttstart(String topic) {
		// 创建MQTT对象
		MQTT mqtt = new MQTT();
		Thing thing;
		try {
			Topic[] topictt = { new Topic(topic, QoS.EXACTLY_ONCE),
					new Topic(topic, QoS.AT_LEAST_ONCE),
					new Topic(topic, QoS.AT_MOST_ONCE) };
			// 设置mqtt broker的ip和端口
			mqtt.setHost(CONNECTION_STRING);
			// 连接前清空会话信息
			mqtt.setCleanSession(CLEAN_START);
			mqtt.setClientId(Double.valueOf(Math.random()*1000000).intValue() + "");
			// 设置重新连接的次数
			mqtt.setReconnectAttemptsMax(RECONNECTION_ATTEMPT_MAX);
			// 设置重连的间隔时间
			mqtt.setReconnectDelay(RECONNECTION_DELAY);
			// 设置心跳时间
			mqtt.setKeepAlive(KEEP_ALIVE);
			// 设置缓冲的大小
			mqtt.setSendBufferSize(SEND_BUFFER_SIZE);
			// 获取mqtt的连接对象BlockingConnection
			final FutureConnection connection = mqtt.futureConnection();
			connection.connect();
			connection.subscribe(topictt);//订阅主题
			Future<Message> futrueMessage = connection.receive();
			Message message = futrueMessage.await();
			String data = new String(message.getPayload());//消息内容
			JSONObject jsStr = JSONObject.parseObject(data);
			int dataTimes = Integer.parseInt(jsStr.getString("DataTimes"));
			int pressure = Integer.parseInt(jsStr.getString("pressure"));
			int humidity = Integer.parseInt(jsStr.getString("humidity"));
			int temper = Integer.parseInt(jsStr.getString("temper"));
			int lux = Integer.parseInt(jsStr.getString("lux"));
			thing = new Thing(pressure,humidity,dataTimes,temper,lux);
			String strplbuf = String.valueOf(message.getPayloadBuffer());//消息内容
			System.out.println("---" + data + "---" + strplbuf);

//			int isok = userDao.saveMsg(thing);


		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(String.valueOf(new Date()) + "--MQTTconning!:"
					+ e.getMessage());
			thing = null;
		}

		return thing;
	}

}
